package StudentLesson;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "AddCourseWorkServlet")
public class AddCourseWorkServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("GBK");
        request.setCharacterEncoding("GBK");
        //时间处理
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date=new Date(System.currentTimeMillis());
        String submitTime=simpleDateFormat.format(date);

        SmartUpload su=new SmartUpload();
        su.initialize(getServletConfig(),request,response);
        su.setMaxFileSize(1024*1024*10);
        try {
            su.upload();
            //获取数据
            String courseNo=su.getRequest().getParameter("courseNo");
            String studentNo=su.getRequest().getParameter("studentNo");
            String workerNo=su.getRequest().getParameter("workerNo");
            String filePath=su.getRequest().getParameter("filePath");
            int courseId=Integer.parseInt(su.getRequest().getParameter("courseId"));
            String workDesc=su.getRequest().getParameter("workDesc");

            //获取上传的文件
            com.jspsmart.upload.File tempFile = su.getFiles().getFile(0);
            //文件夹创建
            File file=new File(filePath);
            if(!file.exists()){ //如果文件夹不存在
                file.mkdirs();
            }
            String fileName=tempFile.getFileName();
            String filePathS=filePath+"/"+fileName;
            //执行插入
            StuCourseWorkDao stuCourseWorkDao=new StuCourseWorkDao();
            int row=stuCourseWorkDao.addStuLesson(courseNo,workerNo,courseId,filePathS,submitTime,studentNo,workDesc);
            int n=su.save(filePath);
            if(row>0){
                response.getWriter().println("插入成功"+n);
            }else {
                response.getWriter().println("插入失败"+n);
            }
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }






    }
}
